package com.itheima.sh.h_json_case;

import com.alibaba.fastjson.JSON;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@WebServlet("/jsonDemo02Servlet")
public class JsonDemo02Servlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doGet(request, response);
    }


    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //获取请求普通字符串
        String name = request.getParameter("name");
        String age = request.getParameter("age");
        //创建User对象存储参数
        User user = new User(name, age);
        System.out.println("user = " + user);

        //请求是普通字符串，响应数据改成json字符串
        String s = JSON.toJSONString(user);
        response.getWriter().print(s);

    }
}